package com.qf.cqut.controller;

import com.qf.cqut.Service.NoticeService;
import com.qf.cqut.model.pojo.ANotice;
import com.qf.cqut.result.ResponseCode;
import com.qf.cqut.result.ResponseData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@RequestMapping("notices")
@CrossOrigin("*")
public class NoticeController {
    @Resource
    private NoticeService noticeService;

    @RequestMapping("list")
    @ResponseBody
    public ResponseData<List<ANotice>> queryAll(Integer page, Integer limit){

        List<ANotice> aNoticeList = noticeService.selectByLimit(page,limit);
        Integer count = noticeService.count();
        if(aNoticeList.isEmpty()){
            return new ResponseData<>(ResponseCode.ERROR);
        }
        return new ResponseData<>(ResponseCode.SUCCESS,count,aNoticeList);
    }

    @RequestMapping("conditions")
    @ResponseBody
    public ResponseData<List<ANotice>> queryByConditions(ANotice aNotice){
        List<ANotice> aNoticeList = noticeService.queryByConditions(aNotice);
        return new ResponseData<>(ResponseCode.SUCCESS,aNoticeList);
    }
    @RequestMapping("del")
    @ResponseBody
    public ResponseData delById(String[] ids){
        Integer i = noticeService.delByIds(ids);
        if(i == 0) {
            return new ResponseData(ResponseCode.ERROR);
        }else{
            return new ResponseData(ResponseCode.SUCCESS);
        }
    }

    @RequestMapping("add")
    @ResponseBody
    public ResponseData addAuser(ANotice aNotice){
        aNotice.setCreateTime(LocalDateTime.now());
        Integer i = noticeService.addAnotice(aNotice);

        if(i == 0){
            return new ResponseData<>(ResponseCode.ERROR);
        }else{
            return new ResponseData<>(ResponseCode.SUCCESS);
        }
    }

    @RequestMapping("edit")
    @ResponseBody
    public ResponseData editAuser(ANotice aNotice){
        aNotice.setUpdateTime(LocalDateTime.now());
        Integer i = noticeService.editAnotice(aNotice);
        if(i == 0){
            return new ResponseData<>(ResponseCode.ERROR);
        }else{
            return new ResponseData<>(ResponseCode.SUCCESS);
        }
    }
}
